package com.hongguan.oms.utils;

import java.beans.PropertyDescriptor;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.web.util.WebUtils;

import com.hongguan.oms.common.Const;
import com.hongguan.oms.domain.UserInfo;

/**
 * @author <a href="mailto:gaoxinyi@hongguaninfo.com">gaoxy</a>
 * @version createTime:2010-1-4 ����06:36:01 summary
 */
public class Util {
	
	final static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	final static DateFormat DATE_FORMAT_STAMP = new SimpleDateFormat("yyyyMMddHHmmss");
	final static DateFormat DATE_FORMAT_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	final static DateFormat DATE_FORMAT_SPLIT = new SimpleDateFormat("yyyy/MM/dd");
	
	public static String toNull(String str) {
		if (str == null)
			return null;
		else if (str.equals(""))
			return null;
		else
			return str;
	}
	
	public static String getUserRoleIdFromSession(HttpServletRequest request) {
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Const.SESSION_USER);
		if (user == null)
			return null;
		return user.getRoleId();
	}
	public static UserInfo getUserInfoFromSession(HttpServletRequest request) {
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Const.SESSION_USER);
		if (user == null)
			return null;  
		return user;
	}
	public static String getLoginIdFromSession(HttpServletRequest request) {
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Const.SESSION_USER);
		if (user == null)
			return null;
		return user.getUserId();
	}
	
	public static String getDepartNameSession(HttpServletRequest request) {
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Const.SESSION_USER);
		if (user == null)
			return null;
		return user.getBranchName();
	}

	public static String getUserIdFromSession(HttpServletRequest request) {
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Const.SESSION_USER);
		if (user == null)
			return null;
		return user.getId();
	}

	public static String getUserDepartmentFromSession(HttpServletRequest request) {
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Const.SESSION_USER);
		if (user == null)
			return null;
		return user.getBranchId();
	}
	

	public static boolean isUserId(HttpServletRequest request) {
		if (Util.getUserIdFromSession(request) == null)
			return false;
		else
			return true;
	}

	public static boolean isDepartment(HttpServletRequest request) {
		if (Util.getUserDepartmentFromSession(request) == null)
			return false;
		else
			return true;
	}

	/**
	 * ��request��ȡlong��ֵ
	 * 
	 * @param request
	 * @param param
	 * @param i
	 *            -Ĭ�Ϸ���ֵ
	 */
	public static long getLong(HttpServletRequest request, String param, long i) {
		String str = request.getParameter(param);
		if (str == null || "".equals(str)) {
			return i;
		} else {
			long ii = i;
			try {
				ii = Long.parseLong(str);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return ii;
		}
	}

	public static long getLong(HttpServletRequest request, String param) {
		return getLong(request, param, 0l);
	}

	/**
	 * ��request��ȡint��ֵ
	 * 
	 * @param request
	 * @param param
	 * @param i
	 *            -Ĭ�Ϸ���ֵ
	 */
	public static int getInt(HttpServletRequest request, String param, int i) {
		String str = request.getParameter(param);
		if (str == null || "".equals(str)) {
			return i;
		} else {
			int ii = i;
			try {
				ii = Integer.parseInt(str);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return ii;
		}
	}

	public static int getInt(HttpServletRequest request, String param) {
		return getInt(request, param, 0);
	}

	/**
	 * ��request��ȡboolean��ֵ
	 * 
	 * @param request
	 * @param param
	 * @param i
	 *            -Ĭ�Ϸ���ֵ
	 */
	public static boolean getBoolean(HttpServletRequest request, String param,
			boolean i) {
		String str = request.getParameter(param);
		if (str == null) {
			return i;
		} else {
			boolean ii = i;
			try {
				ii = Boolean.parseBoolean(str);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return ii;
		}
	}

	public static boolean getBoolean(HttpServletRequest request, String param) {
		return getBoolean(request, param, false);
	}

	/**
	 * ���request�д���4�Ĳ�������Domain Object�еĶ�Ӧ����
	 * 
	 * @param request
	 * @param domainObject
	 * @param prefix
	 *            inputǰ׺
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static void setDomainObjectPropertyFromRequest(
			HttpServletRequest request, Object domainObject, String prefix)
			throws Exception {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
		Map requestMap = WebUtils.getParametersStartingWith(request, prefix);
		PropertyDescriptor[] beandesc = PropertyUtils
				.getPropertyDescriptors(domainObject.getClass());
		for (int i = 0; i < beandesc.length; i++) {
			PropertyDescriptor descriptor = beandesc[i];
			String propertyName = descriptor.getName();
			Class propertyClass = descriptor.getPropertyType();
			if ("class".equals(propertyName)) {
				continue;
			}
			if (propertyClass == Date.class) {
				String value = (String) requestMap.get(propertyName);
				if (value != null && value.trim().length() > 0) {
					Date valueDate = null;
					if (value.trim().length() > 10) {
						valueDate = dateFormat.parse(value);
					} else {
						valueDate = dateFormat2.parse(value);
					}
					PropertyUtils.setProperty(domainObject, propertyName,
							valueDate);
				}
			} else if (propertyClass == String.class) {
				String value = (String) requestMap.get(propertyName);
				if (value != null && value.trim().length() > 0) {
					PropertyUtils
							.setProperty(domainObject, propertyName, value);
				}
			} else if (propertyClass == Integer.class
					|| propertyClass == int.class) {
				String value = (String) requestMap.get(propertyName);
				if (value != null && value.trim().length() > 0) {
					PropertyUtils.setProperty(domainObject, propertyName,
							Integer.parseInt(value));
				}
			} else if (propertyClass == Long.class
					|| propertyClass == long.class) {
				String value = (String) requestMap.get(propertyName);
				if (value != null && value.trim().length() > 0) {
					PropertyUtils.setProperty(domainObject, propertyName, Long
							.parseLong(value));
				}
			} else if (propertyClass == Double.class
					|| propertyClass == double.class) {
				String value = (String) requestMap.get(propertyName);
				if (value != null && value.trim().length() > 0) {
					PropertyUtils.setProperty(domainObject, propertyName,
							Double.parseDouble(value));
				}
			} else if (propertyClass == Float.class
					|| propertyClass == float.class) {
				String value = (String) requestMap.get(propertyName);
				if (value != null && value.trim().length() > 0) {
					PropertyUtils.setProperty(domainObject, propertyName, Float
							.parseFloat(value));
				}
			} else if (propertyClass == Boolean.class
					|| propertyClass == boolean.class) {
				String value = (String) requestMap.get(propertyName);
				if (value != null && value.trim().length() > 0) {
					PropertyUtils.setProperty(domainObject, propertyName,
							Boolean.parseBoolean(value));
				}
			}
		}

	}

	/**
	 * ���request�д���4�Ĳ�������Domain Object�еĶ�Ӧ����
	 * 
	 * @param request
	 * @param domainObject
	 * @throws Exception
	 */
	public static void setDomainObjectPropertyFromRequest(
			HttpServletRequest request, Object domainObject) throws Exception {
		setDomainObjectPropertyFromRequest(request, domainObject, "");
	}

	public static String dateToString(Date date) {
		return DATE_FORMAT.format(date);
	}
	
	public static Date stringToDate(String date) throws ParseException {
		return DATE_FORMAT_HH_MM_SS.parse(date);
	}

	public static String getFileSufix(String filename) {
		int index = filename.lastIndexOf(".");
		if (index == -1) return "unknow";
		return filename.substring(index);
	}
	
	public static String getDateSplit() {
		return DATE_FORMAT_SPLIT.format(new Date());
	}
	public static String getDateStamp() {
		return DATE_FORMAT_STAMP.format(new Date());
	}
	public static String formatDate(String type){
		SimpleDateFormat sdf = new SimpleDateFormat(type);
		return sdf.format(new Date());
	}

}
